<template>
  <div class="user-cert-manage">
    <el-form>
      <el-form-item>
        <el-button type="primary" @click="openEditor()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="certList" height="450px" border>
      <el-table-column label="序号" type="index" align="center"></el-table-column>
      <el-table-column label="证件类型" prop="certType" align="center"></el-table-column>
      <el-table-column label="证件名称" prop="certName" align="center"></el-table-column>
      <el-table-column label="证件号码" prop="certNumber" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="openViewer(scope.row.id)" type="text">详细</el-button>
          <el-button @click="openEditor(scope.row.id)" type="text">编辑</el-button>
          <el-button @click="deleteById(scope.row.id)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <cert-editor ref="certEditor" @refresh="loadCertList()" />
  </div>
</template>

<script>
import CertEditor from './cert-editor'
export default {
  name: 'userCertManage',
  components: {
    CertEditor
  },
  data() {
    return {
      certList: []
    }
  },
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  methods: {
    openViewer(id) {
      this.$refs.certEditor.open({ id: id, readonly: true })
    },
    openEditor(id) {
      this.$refs.certEditor.open({
        id: id || '',
        readonly: false,
        userId: this.userId
      })
    },
    async loadCertList() {
      let res = await this.$http({
        method: 'post',
        url: this.$http.adornUrl(
          '/usercenter-admin/certManage/getCertListByUserId'
        ),
        data: { userId: this.userId }
      })

      this.certList = res.data
    },
    async deleteById(id) {
      let confirm = await this.$confirm('您确认删除此条证件吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确认'
      })

      if (confirm !== 'confirm') return

      let res = await this.$http.post(
        '/usercenter-admin/certmanage/deletebyid',
        { id: id }
      )
      if (res && res.code === 200) {
        this.$notify.success({ title: '成功', message: '删除人员证件成功' })
        this.$store.dispatch('user/loadAlertListCount')
        await this.loadCertList()
      } else {
        this.$notify.error({ title: '错误', message: res.msg })
      }
    }
  },
  mounted() {
    this.loadCertList()
  },
  computed: {
    certTypeList() {
      return this.$store.getters['user/certTypeList']
    }
  }
}
</script>

<style>
</style>
